#include "StdAfx.h"
#include "CIckdXml.h"

CIckdXml * CIckdXml::s_HPXmler = NULL;


CIckdXml::CIckdXml(void)
{
	 m_tiny = new TiXmlDocument();
}

CIckdXml::~CIckdXml(void)
{
}

CIckdXml * CIckdXml::GetXmler()
{
	if(s_HPXmler == NULL)
	{
		s_HPXmler = new CIckdXml();
	}

	return s_HPXmler;
}


bool CIckdXml::LoadXmlFile(vector<CCompanyItem > &paramComp, const string &filePath,TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN*/ )
{
	if(m_tiny == NULL)
		m_tiny = new TiXmlDocument();

	bool result = m_tiny->LoadFile(filePath.c_str(),encoding);
	
	paramComp = GetAllCompany();

	m_tiny = NULL;
	return result;
}

bool CIckdXml::LoadXmlDocument( const string &xmlDocument,TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN*/ )
{
	if(m_tiny == NULL)
		m_tiny = new TiXmlDocument();

	char *bufferTmp = new char[xmlDocument.length()];
	strcpy(bufferTmp,xmlDocument.c_str());

	bool result = m_tiny->LoadBuffer(bufferTmp,(long)xmlDocument.length(),encoding);

	bufferTmp = NULL;
	delete[] bufferTmp;

	return result;
}

vector<CCompanyItem > CIckdXml::GetAllCompany()
{
	vector<CCompanyItem > results;

	TiXmlElement* rootElement = m_tiny->RootElement();  

	string CompanyNun = rootElement->FirstChildElement("Num")->GetText();

	TiXmlElement* Items = rootElement->FirstChildElement("Items");
	TiXmlElement *itemTmp = Items->FirstChildElement("item");
	while(itemTmp)
	{
		CCompanyItem CompItem;
		CompItem.code = itemTmp->FirstChildElement("code")->GetText();
		CompItem.name = itemTmp->FirstChildElement("name")->GetText();

		results.push_back(CompItem);

		itemTmp = itemTmp->NextSiblingElement();
	}
	
	return results;
}

CListDataItem CIckdXml::GetOneceListData()
{
	CListDataItem result;

	TiXmlElement* rootElement = m_tiny->RootElement();  

	TiXmlElement* nextElement = rootElement->FirstChildElement();
	if(nextElement)
	{
		result.status = rootElement->FirstChildElement("status")->GetText();
		result.message = rootElement->FirstChildElement("message")->GetText();
		result.errCode = rootElement->FirstChildElement("errCode")->GetText();
		result.html = rootElement->FirstChildElement("html")->GetText();
		result.mailNo = rootElement->FirstChildElement("mailNo")->GetText();
		result.expTextName = rootElement->FirstChildElement("expTextName")->GetText();
		result.expSpellName = rootElement->FirstChildElement("expSpellName")->GetText();
		//result.sign = rootElement->FirstChildElement("sign")->GetText();
		//result.paisong = rootElement->FirstChildElement("paisong")->GetText();
		//result.tuihui = rootElement->FirstChildElement("tuihui")->GetText();
		result.update = rootElement->FirstChildElement("update")->GetText();
		result.cache = rootElement->FirstChildElement("cache")->GetText();
		result.ord = rootElement->FirstChildElement("ord")->GetText();

		TiXmlElement* Items = rootElement->FirstChildElement("data");
		TiXmlElement *itemTmp = Items->FirstChildElement("item");
		while(itemTmp)
		{
			COneceMessage oneceItem;
			oneceItem.startTime = itemTmp->FirstChildElement("time")->GetText();
			oneceItem.message = itemTmp->FirstChildElement("context")->GetText();

			result.messageList.push_back(oneceItem);

			itemTmp = itemTmp->NextSiblingElement();
		}
	}

	m_tiny->Clear();
	return result;
}

vector<CListDataItem > CIckdXml::GetListDatas()
{
	vector<CListDataItem > results;

	return results;
}
